package com.ericsson.enap.modules.ebmLog.task;

import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @description: 多线程执行sheel脚本任务
 * @author: wangjingshi
 * @create: 2021-04-20 10:39
 */
public class EbmExecuteSheelTask implements Runnable {

    private Logger log = LoggerUtils.Logger(LogFileName.EbmLogJob);
    private static final String EbmShellName = "jxebm.sh";
    private String eventType;
    private CountDownLatch latch;
    private File file;
    private String localPath;
    private String netid;
    private String ebmPath;

    public EbmExecuteSheelTask(String eventType, CountDownLatch latch, File file, String localPath, String netid, String ebmPath) {
        this.eventType = eventType;
        this.latch = latch;
        this.file = file;
        this.localPath = localPath;
        this.netid = netid;
        this.ebmPath = ebmPath;
    }

    @Override
    public void run() {
        log.info("======== start ======="+eventType);
        //筛选出以该EBM事件类型的文件列表
        String[] dir = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(eventType.replace("-", "."))) {
                    return true;
                }
                return false;
            }
        });
        if(dir.length!=0) {
            //按照配置文件均分这批文件(提高性能、效率)
            List<String> eventByTypeList= new ArrayList<>(Arrays.asList(dir));
            log.info("==="+eventType+"========eventByTypeList files ======"+netid+"====="+eventByTypeList.size());
            try {
                String xmlPath = ebmPath + "xml" + File.separator;
                String ebmshellName = ebmPath + eventType + "_" + EbmShellName;
                String event_Type = eventType.split("-")[0];
                File filedir = new File(localPath);
                if (filedir.exists()) {
                    File file = new File(ebmshellName);
                    file.createNewFile();
                    FileOutputStream fop = new FileOutputStream(file);
                    StringBuffer sb = new StringBuffer();
                    sb.append("#!/bin/sh\n");
                    for (String fileName : eventByTypeList) {
                        sb.append("perl ");
                        sb.append("/opt/Ericsson/core/bin/parse_ebm_log.pl");
                        sb.append(" -e "+event_Type);
                        sb.append(" -x "+xmlPath +" -f ");
                        sb.append(localPath);
                        sb.append(fileName);
                        sb.append(" >> ");
                        sb.append(localPath);
                        sb.append(eventType);
                        sb.append(".log\n");
                    }
                    //System.out.println("============"+sb.toString()+" ===type:"+eventType);
                    byte[] contentInBytes = sb.toString().getBytes();
                    fop.write(contentInBytes);
                    fop.flush();
                    fop.close();
                    Process proc = null;
                    proc =Runtime.getRuntime().exec("chmod 777 "+ebmshellName);
                    proc.waitFor();
                    proc = Runtime.getRuntime().exec(ebmshellName);
                    proc.waitFor();
                    //log.info("==== execute combined shells END("+eventType+") ===== netId = " + netid);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                latch.countDown();
                //log.info("======finally====== "+eventType);
            }
        }else{
            latch.countDown();
            //log.info("======else====== "+eventType);
        }
    }




}
